package com.example.newncee.ui.login;

import android.annotation.SuppressLint;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.newncee.MainActivity;
import com.example.newncee.R;
import com.example.newncee.TestBeforeActivity;
import com.example.newncee.data.UserTAG;

/**
 * @author Sora
 * time: 2022/2/11 22:36
 * description: application startup, activity for login
 * user info is stored here using "SharedPreferences"
 */
public class LoginActivity extends AppCompatActivity {
    SharedPreferences sp;   //obj to store user info
    String province="";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        sp = getSharedPreferences("user",MODE_PRIVATE);  // set private mode
        String storedUser = sp.getString(UserTAG.USERNAME.getTag(), "");

        @SuppressLint("CommitPrefEdits") final SharedPreferences.Editor et = sp.edit();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        if (!storedUser.equals("")) {
            // user is not empty, goto MainActivity
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            return;
        }

        final EditText usernameEditText = findViewById(R.id.username);
        final EditText scoreEditText = findViewById(R.id.score);
        final Spinner spinner=findViewById(R.id.spi_province);
        final RadioGroup radioGroup=findViewById(R.id.rdg_course);
        final RadioButton course=findViewById(radioGroup.getCheckedRadioButtonId());
        final Button loginButton = findViewById(R.id.login);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] provinces=getResources().getStringArray(R.array.provinces);
                province=provinces[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        loginButton.setOnClickListener(v -> {
            String username = usernameEditText.getText().toString();
            String score = scoreEditText.getText().toString();
            if (username.equals("") || score.equals(""))
                Toast.makeText(getApplicationContext(), "姓名和分数不能为空！", Toast.LENGTH_LONG).show();
            else if (province.equals(""))
                Toast.makeText(getApplicationContext(), "请选择省份！", Toast.LENGTH_LONG).show();
            else {
                // store user info
                et.putString(UserTAG.USERNAME.getTag(), username);
                et.putString(UserTAG.SCORE.getTag(), score);
                et.putString(UserTAG.PROVINCE.getTag(), province);
                et.putString(UserTAG.COURSE.getTag(), course.getText().toString());
                et.apply();

                // jump to test-before
                startActivity(new Intent(LoginActivity.this, TestBeforeActivity.class));
            }
        });
    }
}